﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using Top.Data;
using System.Threading.Tasks;
using Top.Web;
using Top.Core;
using Top.Web.Api;
using Top.Business.OAuth;

//using System.Net.Http;

namespace Top.PointStore.Business
{
    /*功能说明:登陆操作管理类
    *版本:
    *参与人:王士锋
    *修改日期:2015.3.4
    */
    public static class LoginManager
    {
        /// <summary> 经纪人登陆
        /// <para>1:登陆成功</para>
        /// <para>0:登陆异常</para>
        /// <para>-1:用户名不能为空</para>
        /// <para>-2:密码不能为空</para>
        /// <para>-3:用户不存在</para>
        /// <para>-4:密码错误</para>
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        //public static async Task<T_Broker> Login(string userName, string password,bool dontuse)
        //{
        //    if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
        //    {
        //        return null;
        //    }
        //    var phoneInfo=PhoneInfo.Parse(userName);
        //    if (phoneInfo.IsValid)
        //    {
        //        userName = phoneInfo.LocalNumber;
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //    password = WebHelper.DecryptAES(password);
        //    password = Convert3.ToMD5(password);
        //    Guid g_password  = Convert3.To<Guid>(password);
        //    OAuthAccessTokens accessToken = await OAuth.Login(userName, g_password, true);
        //    const string sql = "select Kid,F_PassWord from T_Broker where  F_Phone={0} limit 1";
        //    using (var db = new DbTour())
        //    {
        //        try
        //        {
        //            var brokeModel = await db.Sql(sql, userName).FirstOrDefault<T_Broker>();
        //            if (brokeModel == null)
        //            {
        //                return null;
        //            }
        //            password = WebHelper.DecryptAES(password);
        //            //var passworda = WebHelper.MD5Encrypt(password);
        //            if (brokeModel.F_PassWord == password)
        //            {
        //                return brokeModel;
        //            }
        //            else
        //            {
        //                return null;
        //            }
        //        }
        //        catch (Exception e)
        //        {
        //            Trace.Write(e, "经纪人登陆异常");
        //            return null;
        //        }
        //    }
        //}

        public static async Task<OAuthUser> Login(string userName, string password)
        {
            if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
            {
                return null;
            }
            var phoneInfo = PhoneInfo.Parse(userName);
            if (phoneInfo.IsValid)
            {
                userName = phoneInfo.LocalNumber;
            }
            else
            {
                return null;
            }
            const string sql = "select Kid,F_PassWord from T_Broker where  F_Phone={0} limit 1";
            using (var db = DataBaseContext.GetDataBase("reader"))
            {
                try
                {
                    var brokeModel = await db.Sql(sql, userName).FirstOrDefault<T_Broker>();
                    if (brokeModel == null)
                    {
                        return null;
                    }
                    password = WebHelper.DecryptAES(password);
                    //password = WebHelper.DecryptAES(password);
                    //var passworda = WebHelper.MD5Encrypt(password);
                    if (brokeModel.F_PassWord == password)
                    {
                        return OAuthHelper.GetUserByBrokerKid(brokeModel.Kid);
                        //return brokeModel;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception e)
                {
                    Trace.Write(e, "经纪人登陆异常");
                    return null;
                }
            }
        }
        public static async Task<bool> IsInDistribute(OAuthUser user)
        {
            if (user == null || !user.IsBroker || user.BrokerID == 0)
                return false;
            using (var db = new DbTour("sqldefault"))
            {
                const string sql = @"
select F_IsDistribute from T_Broker a left join T_BrokerCompany b on a.F_BrokerCompanyKid = b.Kid
where A.Kid = {0}
";
                return await db.Sql(sql, user.BrokerID).ExecuteScalar<int>(0)>0?true:false;
            }
        }

        public static async Task<int> GetCRMCityKid(OAuthUser user)
        {
            using (var db = new DbTour("sqldefault"))
            {
                const string sql = @"select top 1  c.F_CityKid from S_Admin a left join S_AdminRole b on a.Kid=b.F_AdminKid
left join S_Framework c on b.F_FrameworkKid=c.Kid
where a.Kid={0}";
                return await db.Sql(sql, user.CrmUserID).ExecuteScalar<int>(0);
            }
        }
    }
}
